package com.sitech.portal.bigScreen.service.impl;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.bigScreen.dao.ScreenLogicalDao;
import com.sitech.portal.bigScreen.domain.ScreenLogicalPanel;
import com.sitech.portal.bigScreen.domain.TbScreenLogical;
import com.sitech.portal.bigScreen.form.ScreenLogicalPanelForm;
import com.sitech.portal.bigScreen.form.TbScreenLogicalForm;
import com.sitech.portal.bigScreen.service.IScreenLogicalService;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * Created by Administrator on 2015/4/22.
 */
public class ScreenLogicalServiceImpl implements IScreenLogicalService {
    private ScreenLogicalDao screenLogicalDao;
    @Override
    public void addOrUpdateScreenLogical(TbScreenLogicalForm tbScreenLogical,List<ScreenLogicalPanelForm> panels) {
        if(StringUtils.isNotEmpty(tbScreenLogical.getLogicalScreenId())){
            screenLogicalDao.updateScreenLogical(tbScreenLogical.toDomain());
            screenLogicalDao.unrelatedPanelToScreen(tbScreenLogical.getLogicalScreenId());
            for (ScreenLogicalPanelForm panel : panels) {
                panel.setLogicalScreenId(tbScreenLogical.getLogicalScreenId());
                screenLogicalDao.relatedPanelToScreen(panel.toDomain());
            }
        }else{
            tbScreenLogical.setLogicalScreenId(UUID.randomUUID().toString());
            screenLogicalDao.addScreenLogical(tbScreenLogical.toDomain());
            for (ScreenLogicalPanelForm panel : panels) {
                panel.setLogicalScreenId(tbScreenLogical.getLogicalScreenId());
                screenLogicalDao.relatedPanelToScreen(panel.toDomain());
            }
        }
    }

    @Override
    public IPage queryScreenLogicalByFilter(String physicalScreenId,TbScreenLogicalForm form, PageForm pageForm) {
        IPage page ;
        page=screenLogicalDao.queryScreenLogicalByFilter(form.toDomain(),physicalScreenId,pageForm);
        Collection rows = page.getRows();
        List<TbScreenLogicalForm> formList=new ArrayList<TbScreenLogicalForm>();
        for (Object row : rows) {
            formList.add(((TbScreenLogical)row).toForm());
        }
        page.setRows(formList);
        return page;
    }

    @Override
    public void deleteScreenLogicalById(String ids) {
        if(StringUtils.isNotEmpty(ids)){
            String[] idArray=ids.split("!!");
            for (String id : idArray) {
                this.screenLogicalDao.deleteScreenLogicalById(id);
                screenLogicalDao.unrelatedPanelToScreen(id);
            }
        }
    }

    @Override
    public Map<String,Object> queryLogicalScreenById(String id) {
        TbScreenLogical tbScreenLogical = this.screenLogicalDao.queryLogicalScreenById(id);
        Map<String,Object> result=new HashMap<String, Object>();
        result.put("form",tbScreenLogical!=null?tbScreenLogical.toForm():new TbScreenLogicalForm());
        result.put("panels",this.screenLogicalDao.queryScreenLogicalPanelByScreenId(id));
        return result;
    }

    @Override
    public boolean ifNameExist(TbScreenLogicalForm tbScreenLogical) {
        return screenLogicalDao.ifNameExist(tbScreenLogical.toDomain());
    }

    @Override
    public List queryByPhysicalScreenId(String physicalScreenId) {
        if(physicalScreenId!=null&&physicalScreenId.length()>2){
            boolean ifHasPanel="!!".equals(physicalScreenId.substring(0,2));
            if(ifHasPanel){
                physicalScreenId=physicalScreenId.substring(2);
            }
            List<TbScreenLogical> tbScreenLogicals = screenLogicalDao.queryByPhysicalScreenId(physicalScreenId);
            if(!ifHasPanel){
                List<TbScreenLogicalForm> tbScreenLogicalForms = new ArrayList<TbScreenLogicalForm>();
                for (TbScreenLogical tbScreenLogical : tbScreenLogicals) {
                    tbScreenLogicalForms.add(tbScreenLogical.toForm());
                }
                return tbScreenLogicalForms;
            }else{
                List<Map<String,Object>> tbScreenLogicalForms = new ArrayList<Map<String,Object>>();
                for (TbScreenLogical tbScreenLogical : tbScreenLogicals) {
                    Map<String,Object> form=new HashMap<String, Object>();
                    form.put("layout",tbScreenLogical.getLAYOUT());
                    form.put("logicalScreenName",tbScreenLogical.getLOGICAL_SCREEN_NAME());
                    form.put("logicalScreenId",tbScreenLogical.getLOGICAL_SCREEN_ID());
                    form.put("showSeconds",tbScreenLogical.getSHOW_SECONDS());
                    form.put("switchAnimation",tbScreenLogical.getSWITCH_ANIMATION());
                    form.put("panels",screenLogicalDao.queryScreenLogicalPanelByScreenId(tbScreenLogical.getLOGICAL_SCREEN_ID()));
                    tbScreenLogicalForms.add(form);
                }
                return tbScreenLogicalForms;
            }

        }else{
            return Collections.emptyList();
        }

    }

    @Override
    public List<String> queryPhysicalScreenByLogicalScreenId(String logicalScreenId) {
        return screenLogicalDao.queryPhysicalScreenByLogicalScreenId(logicalScreenId);
    }

    public void setScreenLogicalDao(ScreenLogicalDao screenLogicalDao) {
        this.screenLogicalDao = screenLogicalDao;
    }
}
